package com.shigen.shop.inteceptor;

import com.shigen.shop.entity.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登录拦截器
 *
 * @author shigenfu
 * @date 2022/7/25 11:44 下午
 */

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

  public static ThreadLocal<User> threadLocal = new ThreadLocal<>();


  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    String accessToken = request.getHeader("token");
    if (accessToken == null) {
      accessToken = request.getParameter("token");
    }

    User xiaofu = new User().setId(10L).setName("xiaofu");

    threadLocal.set(xiaofu);

    return true;

  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

  }

}
